package 哈希表;

import java.util.HashMap;
import java.util.HashSet;

public class 数组中出现次数超过一半的数字 {
    public int MoreThanHalfNum_Solution(int [] array){
        if (array == null || array.length == 0) return 0;
        if (array.length == 1) return array[0];
        int mid = array.length/2;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if (!map.containsKey(array[i])){
                map.put(array[i],1);
            }else if (map.get(array[i])<mid){
                map.put(array[i],map.get(array[i])+1);
            }else {
                return array[i];
            }
        }
        return 0;
    }
}
